package com.lab303.powerproject.util;

import com.csvreader.CsvReader;
import org.apache.commons.lang3.time.DateUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class ReadCsv {

    public static List<String[]> readCsv(String csvFilePath) throws IOException {
        ArrayList<String[]> csvList = new ArrayList<String[]>(); //用来保存数据
        //注明：或者可以通过前端上传的文件，用一个方法获取上传文件名uploadName(String)
        CsvReader reader = new CsvReader(csvFilePath, ',', Charset.forName("UTF-8"));
        //解决中文编码
        reader.readHeaders();// 跳过表头   如果需要表头的话，不要写这句。
        while (reader.readRecord()) { //逐行读入除表头的数据
            csvList.add(reader.getValues());
        }
        reader.close();
        return csvList;
    }

    public static void main(String[] args) throws IOException, ParseException {
        List<String[]> readCsv = ReadCsv.readCsv("C:\\Users\\DragonGao\\AppData\\Roaming\\Notepad++\\plugins\\config\\NppFTP\\Cache\\root@49.234.228.157\\home\\power\\uploadfile\\aj\\data\\prehour.csv");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, 1);
        date = calendar.getTime();
        List<String> list = new ArrayList<>();
        for (String[] strs:readCsv){
            String dateStr = strs[0];
            dateStr = dateStr.split("  ")[0];
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            Date date1 = simpleDateFormat.parse(dateStr);
            if (DateUtils.isSameDay(date1,date)){
                list.add(strs[4]);
            }
        }
        int count = 0;
        for (String s:list){
            System.out.print(s+"  ");
            if ("Sunny.".equals(s)){
                count++;
            }
        }
    }
}
